<!DOCTYPE html>
<html debug="true">
  <head>
    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>

    <script type="text/javascript">
        var printCapabilities = {"scales":[{"name":"1:25,000","value":"25000"},{"name":"1:50,000","value":"50000"},{"name":"1:100,000","value":"100000"},{"name":"1:200,000","value":"200000"},{"name":"1:500,000","value":"500000"},{"name":"1:1,000,000","value":"1000000"},{"name":"1:2,000,000","value":"2000000"},{"name":"1:4,000,000","value":"4000000"}],"dpis":[{"name":"75","value":"75"},{"name":"150","value":"150"},{"name":"300","value":"300"}],"layouts":[{"name":"A4 portrait","map":{"width":440,"height":483},"rotation":true},{"name":"Legal","map":{"width":440,"height":483},"rotation":false}],"printURL":"http://demo.opengeo.org/geoserver/pdf/print.pdf","createURL":"http://demo.opengeo.org/geoserver/pdf/create.json"};
       
        function test_initComponent(t) {
            t.plan(9);
            
            var mapPanel = new GeoExt.MapPanel({
                renderTo: "mappanel",
                width: 256,
                height: 256,
                layers: [new OpenLayers.Layer.WMS("Tasmania State Boundaries",
                    "http://demo.opengeo.org/geoserver/wms",
                    {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
                center: [146.56, -41.56],
                zoom: 7
            });
            
            var log = {};
            var printMapPanel = new GeoExt.PrintMapPanel({
                renderTo: "printmappanel",
                map: {
                    controls: [new OpenLayers.Control.PanPanel()],
                    eventListeners: {
                        preaddlayer: function(e) {
                            log.preaddlayer = e.layer;
                        }
                    }
                },
                printProvider: {
                    capabilities: printCapabilities
                },
                sourceMap: mapPanel
            });
            
            var size = printMapPanel.printProvider.layout.get("size");
            var center = mapPanel.map.getCenter();
            var pageCenter = printMapPanel.printPage.center;
            
            t.eq(printMapPanel.map.controls.length, printMapPanel.map.controls[0].controls.length+1, "Preview map has only the control we added,");
            t.ok(printMapPanel.map.controls[0] instanceof OpenLayers.Control.PanPanel, "which is a PanPanel.");
            t.eq(printMapPanel.map.getResolution(), mapPanel.map.getResolution(), "Preview map has the same resolution as the source map.");
            t.ok(printMapPanel.map.getCenter().equals(center), "Preview map has the same center as the source map.");
            t.eq(printMapPanel.map.layers[0].name, mapPanel.map.layers[0].name, "Preview map has the same visible layer as the source map.");
            t.eq(log.preaddlayer.name, printMapPanel.map.layers[0].name, "preaddlayer listener noticed addition of the layer.");
            t.eq((printMapPanel.width/printMapPanel.height).toPrecision(8), (size.width/size.height).toPrecision(8), "Aspect ratio of the preview map is the same as of the print layout.");
            t.ok(Math.abs(center.lon - pageCenter.lon) < 0.0000001 && Math.abs(center.lat - pageCenter.lat) < 0.0000001, "Print page has the same center as the source map.");
            t.eq(printMapPanel.printPage.scale.get("value"), 2000000, "Print page has the correct scale to fit the source map.");
            
            printMapPanel.destroy();
            mapPanel.destroy();
        }

    </script>
  <body>
    <div id="mappanel"></div>
    <div id="printmappanel"></div>
  </body>
</html>
